package cn.wbnull.springbootdemo.controller;

import java.io.IOException;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;

import cn.wbnull.springbootdemo.model.Users;
import cn.wbnull.springbootdemo.service.UserService;
import cn.wbnull.springbootdemo.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;

/**
 * excel导入导出
 *
 * @author EDZ
 * @since 2022-04-14
 */
@RestController
@RequestMapping("/excel")
@Slf4j
public class Excel {

    @Autowired
    private UserService userService;

//    @RequestMapping("/import")
//    public List<Users> noEntity(MultipartFile file) throws IOException {
//        List<Object> list = EasyExcel.read(file.getInputStream()).sheet(0).doReadSync();
//        List<Users> list1 = new ArrayList<>();
//
//
//        List<Users> bmsqList = (List<Users>)(List)list;
//        System.out.println(bmsqList);
////        List<Users> users = cun(list);
////        System.out.println(users);
//        return bmsqList;
//    }

    //存储数据库
//    public static List<Users> cun(List<Object> list) {
//        List<Users> list1 = new ArrayList<>();
//        System.out.println(list);
//        for (int i = 0; i < list.size(); i++) {
//            Users users = new Users();
//            Object[] obj = (Object[]) list.get(i);
//            users.setId((Integer) obj[0]);
//            users.setName(obj[1].toString());
//            users.setPrice((Integer) obj[2]);
//            users.setAge((Integer) obj[3]);
//            users.setStatus(obj[4].toString());
//            list1.add(users);
//        }
//        System.out.println(list);
//
//
//        return list1;
//    }
    /**
     * 表单导出
     *
     * @param response 会话id
     * @throws IOException 会话id
     */
    @RequestMapping("/derive")
    public void  derive(HttpServletResponse response){

        try {
            userService.derive(response);
        } catch (Exception e) {
            log.warn("excelService IOException error");
        }

    }

    /**
     * 表单导出
     *
     * @param response 会话id
     * @throws IOException 会话id
     */
    @PostMapping("/import")
    public JSONArray importUser(@RequestPart("file")MultipartFile file) throws Exception {
        JSONArray array = ExcelUtils.readMultipartFile(file);
        System.out.println("导入数据为:" + array);
        return array;
    }



}
